package com.mrinalwadhwa.effects.effectClasses
{
	import flash.display.Shader;
	import flash.events.Event;
	import flash.filters.ShaderFilter;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	import mx.effects.Tween;
	import mx.effects.effectClasses.TweenEffectInstance;

	public class BendPixelsInstance extends TweenEffectInstance
	{

		public var benderFileURL:String;
		public var benderParams:Array;
		public var benderByteArray:ByteArray;

		
		private var loader:URLLoader;
		private var shader:Shader;
		private var filter:ShaderFilter;
		
		private var properties:Array;
		private var startValues:Array;
		private var endValues:Array;


		public function BendPixelsInstance(target:Object)
		{
			super(target);
		}


        override public function play():void {
            
            super.play();

			properties = new Array();
			startValues = new Array();
			endValues = new Array();

			for each (var param:Object in benderParams){
				if(param[1] is Array){
					var a:Array = param[1] as Array;
					for (var i:int=0;i<a.length;i++){
						properties.push(param[0]);
						startValues.push(Number(param[1][i]));
						endValues.push(Number(param[2][i]));
					}
					continue;
				}
				properties.push(param[0]);
				startValues.push(Number(param[1]));
				endValues.push(Number(param[2]));
			}
			
			
			if(benderByteArray){
				shader = new Shader(benderByteArray);
				var tween:Tween = createTween(this, startValues, endValues, duration);  
			}else if(benderFileURL){
				loader = new URLLoader();
				loader.dataFormat = URLLoaderDataFormat.BINARY;
				loader.addEventListener(Event.COMPLETE, onLoadComplete);
				loader.load(new URLRequest(benderFileURL));
			}
        }


		private function onLoadComplete(e:Event):void
		{
			shader = new Shader(loader.data);
			var tween:Tween = createTween(this, startValues, endValues, duration);  
		}


 
        override public function onTweenUpdate(values:Object):void {
			setFilter(values);
        }
  
 
        override public function onTweenEnd(values:Object):void {
            setFilter(values);
            super.onTweenEnd(values);
        }
        

		private function setFilter(values:Object):void {
			
			var filters:Array = target.filters;
			var n:int = filters.length;
			for (var i:uint = 0; i < n; i++)
			{
				if (filters[i] is ShaderFilter){
					filters.splice(i, 1);
				}
			}


			var count:uint = 0;
			var len:uint = benderParams.length;
			for (var c:uint;c<len;c++){
					if(benderParams[c][1] is Array){
						var a:Array = benderParams[c][1] as Array;
						var valArr:Array = new Array();
						for (var j:int=0;j<a.length;j++){
							valArr[j] = values[count];
							count++;
						}
						trace("*"+properties[count-a.length]+"*"+(count-a.length));
						shader.data[properties[count-a.length]].value = valArr;
						continue;
					}
					count++;
					shader.data[properties[c]].value = [values[c]];
			}
			
			filter = new ShaderFilter(shader);
			filters.push(filter);
			target.filters = filters;			
		}
        
	}
}
